/**
 * 缺陷：当A、B和C线程同时进行时，若A线程联合到B和C的线程中，若B或C其中一方执行完毕A线程就可以继续运行
 *
 *
 *
 *
 */

import java.util.Random;

import static java.lang.Thread.*;

class ThreadA implements Runnable
{
    //线程调用次数
    private int i = 1;
    
    //定义停止令牌
    private int stop = 0;
    private boolean flag = true;
    
    Random random = new Random();
    
    @Override
    public void run ()
    {
        System.out.println(Thread.currentThread().getName() + " A 启动");
    
        //定义辅助线程
        Thread daemon = new Thread(new Daemon(), "Demon");
        daemon.setDaemon(true);
    
        //启动辅助线程
        daemon.start();
    
        while(flag)
        {
            if("Thread-0".equals(Thread.currentThread().getName()))
                if(i == 1)
                {
                    System.out.println("让步");
                    
                    Thread.yield();
                }
    
            System.out.println(Thread.currentThread().getName() + " A " + i++);
            
            
            if((stop = random.nextInt(20)) == 18)
            {
                stop();
    
                System.out.println("线程结束，总计运行：" + i + "次");
            }
    
            try
            {
                sleep(200);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    //终止线程方法
    public void stop()
    {
        this.flag = false;
    }
}

class ThreadB implements Runnable
{
    private Thread thread;
    
    public ThreadB (Thread thread)
    {
        this.thread = thread;
    }
    
    @Override
    public void run ()
    {
        System.out.println(Thread.currentThread().getName() + " B 启动了");
        
        for(int i = 1; i <= 10; i++)
        {
            System.out.println(Thread.currentThread().getName() + " B " + i);
            
//            if(i == 5)
//            {
//                try
//                {
//                    thread.join();
//                }
//                catch(InterruptedException e)
//                {
//                    e.printStackTrace();
//                }
//            }
    
            try
            {
                sleep(200);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}

class Daemon implements Runnable
{
    @Override
    public void run ()
    {
        System.out.println("辅助线程启动");
        
        for(int i = 1; i < 200; i++)
        {
            System.out.println("辅助线程调用：" + i + "次");
    
            try
            {
                Thread.sleep(500);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}

public class TestRunnable
{
    public static void main (String[] args) throws InterruptedException
    {
        //设置自定义类线程
        Thread forest = new Thread(new ThreadA(), "Name");
        Thread sand = new Thread(new ThreadB(forest), "Name");
        //设置主函数的线程名
        Thread.currentThread().setName("Name");
        
        //设置线程优先级名字
        forest.setPriority(MAX_PRIORITY);
        sand.setPriority(MIN_PRIORITY);
        
        //启动线程
        forest.start();
        sand.start();
    
        for(int i = 1; i <= 10; i++)
        {
            System.out.println(Thread.currentThread().getName() + " V " + i);
            
            if(i == 5)
            {
                //联合线程，该线程不再运行，直至被联合的线程执行完毕
                System.out.println("联合——class_Main");
                
                sand.join();
            }
    
            try
            {
                sleep(200);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}